package com.ts.host;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.easemob.EMCallBack;
import com.easemob.EMConnectionListener;
import com.easemob.EMError;
import com.easemob.chat.EMChat;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMGroupManager;
import com.easemob.util.NetUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.rio.core.BaseApplication;
import com.rio.core.L;
import com.rio.core.U;
import com.rio.layout.LayoutManager;
import com.rio.layout.TaskManager;
import com.rio.layout.view.SimpleTask;
import com.ts.manager.MachineManager;
import com.ts.manager.WaterManager;

import android.app.ActivityManager;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.text.TextUtils;



public class APP extends BaseApplication {

	public static final String TAG = "STH";
	
	public static final String ACTION = "com.ts.host.KILL";
	
	public static MyConnectionListener mMyConnectionListener;
	
	public static final String TEST = "test";

	private static APP app;
	
	public boolean isDownloading = false;
	
	public static APP getInstance(){
		return app; 
	}


	@Override
	public void onCreate() {
		super.onCreate();
		app = this;
		setTag(TAG);
		setDebugMode(true);

		String processAppName = getAppName();
		// 如果app启用了远程的service，此application:onCreate会被调用2次
		// 为了防止环信SDK被初始化2次，加此判断会保证SDK被初始化1次
		// 默认的app会在以包名为默认的process name下运行，如果查到的process name不是app的process name就立即返回
		 
		if (processAppName == null ||!processAppName.equalsIgnoreCase("com.ts.host")) {
		    //"com.easemob.chatuidemo"为demo的包名，换到自己项目中要改成自己包名		 
		    // 则此application::onCreate 是被service 调用的，直接返回
		    return;
		}
		initEasemob();

		
	}



	private void initEasemob() {
		
		EMChat.getInstance().setAutoLogin(false);
		
		EMChat.getInstance().init(getContext());
		 
		/**
		 * debugMode == true 时为打开，sdk 会在log里输入调试信息
		 * @param debugMode
		 * 在做代码混淆的时候需要设置成false
		 */
		EMChat.getInstance().setDebugMode(APP.isDebug());
		

		
		//注册一个监听连接状态的listener
		mMyConnectionListener = new MyConnectionListener();
		EMChatManager.getInstance().addConnectionListener(mMyConnectionListener);
		 

		EMChat.getInstance().setAppInited();
	}





	static void destroy() {
		if(U.notNull(mMyConnectionListener)){
			EMChatManager.getInstance().removeConnectionListener(mMyConnectionListener);
		}

	}
	
	private String getAppName() {
		int pid = android.os.Process.myPid();
		String processName = null;
		ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
		List l = am.getRunningAppProcesses();
		Iterator i = l.iterator();
		PackageManager pm = this.getPackageManager();
		while (i.hasNext()) {
			ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) (i.next());
			try {
				if (info.pid == pid) {
					CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
					processName = info.processName;
					return processName;
				}
			} catch (Exception e) {

			}
		}
		return processName;
	}

	private class MyConnectionListener implements EMConnectionListener {
	    @Override
		public void onConnected() {
	    	//已连接到服务器
	    	WaterManager.getInstance().setMode(WaterManager.MODE_CHARGING);
		}
		@Override
		public void onDisconnected(final int error) {
			L.i("通讯关闭");
			TaskManager.getInstance().async(new SimpleTask() {
	 
				@Override
				public Object onBGThread(Object... params) throws Exception {
					MachineManager.getInstance().unregister();
					WaterManager.getInstance().setMode(WaterManager.MODE_FREE);
					if(error == EMError.USER_REMOVED){
						// 显示帐号已经被移除

					}else if (error == EMError.CONNECTION_CONFLICT) {
						// 显示帐号在其他设备登陆
	
					} else {
						if (NetUtils.hasNetwork(getContext())){
						//连接不到聊天服务器
						}else{
						//当前网络不可用，请检查网络设置
						}
					}
					return null;
				}
			});
		}
	}
}
